module.exports = function ($scope, $state, $resource,$modalInstance, $modal, toaster,item,flag ){
    
   
    var code =  flag ? {'flag': true} : item;
    $scope.tourprice = {
        'code':code
    };

    function date2str(d) {
        if (d === undefined) {
            return "";
        }
        var month = (d.getMonth() + 1).toString();
        var day = d.getDate().toString();
        if (month.length == 1) month = '0' + month;
        if (day.length == 1) day = '0' + day;
        return d.getFullYear() + "-" + month + "-" + day;
    }
    function str2date (strDate){
        console.log(strDate);
        if(angular.isString(strDate))
        {
            var objDate = new Date(Date.parse(strDate.replace(/-/g, "/")));

            return objDate;
        }
        else
        {
            return '错误格式';
        }
        
    }

    $scope.date = {
    //  'lable': date2str(new Date()),
    //  'value': date2str(new Date()),
// 'lable': date2str2(new Date()),
        'opened': false
    }


    $scope.dateOpen = function ($event, item) {
        $event.preventDefault();
        $event.stopPropagation();
        item.opened = true;
    };

    $scope.date1 = {
        //   'lable': date2str(new Date()),
        //  'value': date2str(new Date()),
        'opened': false
    }


    $scope.dateOpen1 = function ($event, item) {
        $event.preventDefault();
        $event.stopPropagation();
        item.opened = true;
    };

    
    
    
    init();
    function init(){
        $scope.tourprice.adult_ban = '0';
        $scope.tourprice.children_ban = '0';
        $scope.tourprice.take_deposit = '1';
        
        $resource('/api/as/goc/tourbase/getPrice', {}, {}).save({'code':code},function (res) {
            
            if (res.errcode == 0) {
                $scope.tourprice = res.data;
                // $scope.tourprice.tour_date_start = str2date($scope.tourprice.tour_date_start);
                // $scope.tourprice.tour_date_end = str2date($scope.tourprice.tour_date_end);
                $scope.tourprice.tour_date_start = str2date('2017-12-4');
                $scope.tourprice.tour_date_end = str2date('2017-12-31');
            }

            

        });

    }
    $scope.save = function(){
       
       
        if($scope.tourprice.tour_date_start == undefined){
            toaster.error({title: '', body: '请选择开始时间！'});
          return;
        }else{
            $scope.tourprice.tour_date_start = date2str($scope.tourprice.tour_date_start);
            console.log($scope.tourprice.tour_date_start);
        }
        if($scope.tourprice.tour_date_end == undefined){
            toaster.error({title: '', body: '请选择结束时间！'});
          return;
        }else{
            $scope.tourprice.tour_date_end = date2str($scope.tourprice.tour_date_end);
            console.log($scope.tourprice.tour_date_end);
        }
        if($scope.tourprice.adult_ban == '1'){
            if($scope.tourprice.adult_call_price == undefined){
                toaster.error({title: '', body: '请输入成人结算价！'});
              return;
            }
        }
        if($scope.tourprice.children_ban == '1'){
            if($scope.tourprice.children_call_price == undefined){
                toaster.error({title: '', body: '请输入儿童结算价！'});
            return;
            }
        }
        if($scope.tourprice.adult_ban == '1'){
            if($scope.tourprice.take_deposit_fee == undefined){
                toaster.error({title: '', body: '请输入占位订金金额！'});
            return;
            }
        }
        // $resource('/api/as/goc/tourbase/savePrice', {}, {}).save($scope.tourprice,function (res) {
           
        //     if(res.errcode === 0){
        //         toaster.success({title: '', body: '保存成功'});
        //         $modalInstance.close();
        //     }else{
        //         toaster.error({title: '', body: res.errmsg});
        //     }
                
        // });
    }
    
    

    
        
}